/**
 * 分类管理路由模块
 * 
 * 实现分类增删改查(CRUD)操作:
 * 1. GET /api/categories?tree=true|false - 获取分类列表
 * 2. GET /api/categories/:id - 根据ID获取特定分类
 * 3. GET /api/categories/:id/children - 获取分类的子级分类
 * 4. POST /api/categories - 创建新分类
 * 5. PATCH /api/categories/:id - 更新分类
 * 6. DELETE /api/categories/:id - 删除分类
 * 7. PATCH /api/categories/:id/sort - 更新分类排序
 */

//引入express模块
const express = require("express");

//创建路由对象
const router = express.Router();

//引入分类控制器
const categoryController = require("../controllers/category.controller");

// 引入公共的异步错误处理工具函数
const asyncHandler = require("../utils/asyncHandler");

/**
 * @route   GET /api/categories
 * @desc    获取分类列表
 * @access  Public
 * @query   tree - 是否获取树形结构 (true|false)
 */
router.get("/", asyncHandler(categoryController.getCategories));

/**
 * @route   GET /api/categories/:id
 * @desc    根据ID获取特定分类
 * @access  Public
 */
router.get("/:id", asyncHandler(categoryController.getCategoryById));

/**
 * @route   GET /api/categories/:id/children
 * @desc    获取分类的子级分类
 * @access  Public
 */
router.get("/:id/children", asyncHandler(categoryController.getCategoryChildren));

/**
 * @route   POST /api/categories
 * @desc    创建新分类
 * @access  Public
 */
router.post("/", asyncHandler(categoryController.createCategory));

/**
 * @route   PATCH /api/categories/:id
 * @desc    更新分类
 * @access  Public
 */
router.patch("/:id", asyncHandler(categoryController.updateCategory));

/**
 * @route   DELETE /api/categories/:id
 * @desc    删除分类
 * @access  Public
 */
router.delete("/:id", asyncHandler(categoryController.deleteCategory));

/**
 * @route   PATCH /api/categories/:id/sort
 * @desc    更新分类排序
 * @access  Public
 */
router.patch("/:id/sort", asyncHandler(categoryController.updateCategorySort));

module.exports = router;